﻿# Called from code after history generation
# Empty scope
on_game_start = {
	effect = {
		### 867 - WRATH OF THE NORTHMEN ###
		#Æthelred dying (probably)
		character:33358 = {
			if = {
				limit = {
					is_alive = yes
					is_landed = yes
				}
				trigger_event = {
					id = bookmark.0001
					days = { 365 730 }
				}
			}
		}

		#Alfred the Great becoming the Great
		character:7627 = {
			if = {
				limit = {
					is_alive = yes
					is_landed = yes
				}
				trigger_event = {
					id = bookmark.0002
					days = 1800 #~5 years
				}
			}
		}

		### 867 - THE GREAT ADVENTURERS ###
		character:251187 = {
			if = {
				limit = {
					is_alive = yes
					is_landed = yes
					AND = {
						character:251180 = { is_ai = yes }
						character:251181 = {
							is_ai = yes
							is_alive = yes
						}
					}
				}
				trigger_event = {
					id = bookmark.0101
					days = { 21 35 }
				}
			}
		}

		### HRE SETUP ###
		if = {
			limit = { exists = title:e_hre.holder }
			title:e_hre.holder = {
				every_vassal = {
					limit = {
						vassal_contract_has_modifiable_obligations = yes
					}
					vassal_contract_decrease_obligation_level = feudal_government_taxes
					vassal_contract_decrease_obligation_level = feudal_government_levies
				}
			}
		}

		#Set up Visigothic Codes.
		culture:occitan = { add_innovation = innovation_visigothic_codes }
		culture:basque = { add_innovation = innovation_visigothic_codes }
		culture:aragonese = { add_innovation = innovation_visigothic_codes }
		culture:catalan = { add_innovation = innovation_visigothic_codes }

		### RELIGIOUS THINGS ###
		religion:islam_religion = { # Set up Holy Site Buildings to be buildable in Holy Sites
			every_faith = {
				every_holy_site = {
					limit = {
						title_province = {
							has_special_building_slot = no
						}
					}
					title_province = {
						add_special_building_slot = holy_site_mosque_01
					}
				}
			}
		}
		
		religion:christianity_religion = {
			every_faith = {
				every_holy_site = {
					limit = {
						title_province = {
							has_special_building_slot = no
						}
					}
					title_province = {
						add_special_building_slot = holy_site_cathedral_01
					}
				}
			}
		}
		
		faith:old_bon = { # Has already been reformed into new bon.
			set_variable = { name = has_been_reformed }
		}

		every_religion_global = {
			limit = {
				OR = {
					this = religion:hinduism_religion
					this = religion:buddhism_religion
					this = religion:jainism_religion
					this = religion:tani_religion
					this = religion:bon_religion
				}
			}
			every_faith = {
				every_holy_site = {
					limit = {
						title_province = {
							has_special_building_slot = no
						}
					}
					title_province = {
						add_special_building_slot = holy_site_indian_grand_temple_01
					}
				}
			}
		}
		
		every_religion_global = {
			limit = {
				is_in_family = rf_pagan
			}
			every_faith = {
				every_holy_site = {
					limit = {
						title_province = {
							has_special_building_slot = no
						}
					}
					title_province = {
						add_special_building_slot = holy_site_pagan_grand_temple_01
					}
				}
			}
		}
		
		every_religion_global = {
			every_faith = {
				every_holy_site = {
					limit = {
						title_province = {
							has_special_building_slot = no
						}
					}
					title_province = {
						add_special_building_slot = holy_site_other_grand_temple_01
					}
				}
			}
		}
	}
}

# Like on_game_start, except it is called once the host (or player, in single player) exits the lobby. Good for anything where you need to know who the players are, or what the game rules are
on_game_start_after_lobby = {
	effect = {
		### GAME RULE: VIEW ON SAME-SEX RELATIONS
		if = {
			limit = { has_game_rule = accepted_same_sex_relations }
			game_rule_accepted_same_sex_relations_effect = yes
		}

		### GAME RULE: RANDOM RULER PLACEMENT
		if = {
			limit = { NOT = { has_game_rule = random_ruler_placement_off } }
			game_rule_random_ruler_placement_effect = yes
		}

		### GAME RULE: RANDOMIZE FAITH
		if = {
			limit = { has_game_rule = randomized_faiths_on }
			game_rule_randomize_faith_effect = yes
		}

		### GAME RULE: FAITH ACCEPTANCE
		if = {
			limit = { has_game_rule = full_faith_acceptance }
			game_rule_faith_acceptance_effect = yes
		}

		### GAME RULE: GENDER EQUALITY ###
		if = {
			limit = { has_game_rule = full_gender_equality }
			game_rule_full_gender_equality_effect = yes
		}
		else_if = {
			limit = { has_game_rule = inversed_gender_equality }
			game_rule_inversed_gender_equality_effect = yes #todo fix
		}

		### GAME RULE: SEXUALITY DISTRIBUTION ###
		if = {
			limit = { NOT = { has_game_rule = sexuality_distribution_default } }
			game_rule_sexuality_distribution_effect = yes
		}

		### GAME RULE: DE JURE EMPIRES ###
		if = {
			limit = { NOT = { has_game_rule = default_empires } }
			mgr_empires_effect = yes
		}

		### GAME RULE: GOVERNMENTS ###
		#Ideally we would do this after shattered realms, but the game doesn't always recognize independent rulers until a day passes.
		if = {
			limit = { NOT = { has_game_rule = default_starting_governments } }
			every_ruler = {
				limit = { 
					OR = {
						is_independent_ruler = yes
						AND = { #Pope vassals
							liege = { has_government = theocracy_government }
							highest_held_title_tier >= tier_county
						}
					}
					OR = {
						NOT = { primary_title = { is_landless_type_title = yes } }
						NOT = { has_government = theocracy_government }
					}
				}
				randomize_government_and_holdings_effect = yes
			}
		}
		
		### GAME RULE: RANDOM REALMS ###
		### TO DO ###
		
		### GAME RULE: SHATTERED REALMS ###
		mgr_shattered_realms_effect = yes 

		### GAME RULE: RANDOM RULERS ###
		if = {
			limit = { has_game_rule = yes_random_rulers }
			mgr_randomize_rulers = yes
		}
		
		### GAME RULES FOR DOMAIN/VASSAL/KNIGHT/MAA/ETC LIMITS
		#We give every ruler a proper modifier. Needs to be re-applied when a character gains a new title
		every_ruler = { apply_limit_modifiers_effect = yes }
		
		### ACHIEVEMENT: FROM RAGS TO RICHES
		every_player = {
			limit = { highest_held_title_tier = tier_county }
			add_achievement_global_variable = {
				VARIABLE = achievement_rags_to_riches_valid
				VALUE = yes
			}
		}
		### ACHIEVEMENT TRACKING FOR STARTING CHARACTERS
		if = { 
			limit = { has_multiple_players = no }
			every_player = { 
				if = {
					limit = { 
						exists = character:7757
						this = character:7757
					}
					add_achievement_global_variable = {
						VARIABLE = started_give_a_dog_a_bone_achievement
						VALUE = yes
					}
				}	
				if = {
					limit = { 
						exists = character:1128
						this = character:1128
					}
					add_achievement_global_variable = {
						VARIABLE = started_wily_as_the_fox_achievement
						VALUE = yes
					}
				}
				if = {
					limit = {
						OR = {
							AND = {
								exists = character:108501
								this = character:108501
							}
							AND = {
								exists = character:107500
								this = character:107500
							}
							AND = {
								exists = character:107501
								this = character:107501
							}
							AND = {
								exists = character:108500
								this = character:108500
							}
							AND = {
								exists = character:109500
								this = character:109500
							}
						}
					}
					add_achievement_global_variable = {
						VARIABLE = started_sibling_rivalry_achievement
						VALUE = yes
					}
				}
				if = {
					limit = {
						OR = {
							AND = {
								exists = character:163108
								this = character:163108
							}
							AND = {
								exists = character:163110
								this = character:163110
							}
							AND = {
								exists = character:163111
								this = character:163111
							}
							AND = {
								exists = character:163112
								this = character:163112
							}
							AND = {
								exists = character:163119
								this = character:163119
							}
						}
					}
					add_achievement_global_variable = {
						VARIABLE = started_blood_eagle_achievement
						VALUE = yes
					}
				}
				if = {
					limit = {
						exists = character:6448
						this = character:6448
					}
					add_achievement_global_variable = {
						VARIABLE = started_kings_to_the_seventh_generation_achievement
						VALUE = yes
					}
				}
				if = {
					limit = {
						exists = character:140
						this = character:140
					}
					add_achievement_global_variable = {
						VARIABLE = started_norman_yoke_achievement
						VALUE = yes
					}
				}
				if = {
					limit = {
						exists = character:522
						this = character:522
					}
					add_achievement_global_variable = {
						VARIABLE = started_royal_dignity_achievement
						VALUE = yes
					}
				}
				if = {
					limit = {
						exists = character:40605
						this = character:40605
					}
					add_achievement_global_variable = {
						VARIABLE = started_land_of_the_rus_achievement
						VALUE = yes
					}
				}
				if = {
					limit = {
						exists = character:251187
						this = character:251187
					}
					add_achievement_global_variable = {
						VARIABLE = started_mother_of_us_all_achievement
						VALUE = yes
					}
				}
				if = {
					limit = {
						OR = {
							culture_group = culture_group:iberian_group
							culture = culture:andalusian
						}
						has_religion = religion:christianity_religion
					}
					add_achievement_global_variable = {
						VARIABLE = started_reconquista_achievement
						VALUE = yes
					}
				}
				if = {
					limit = {
						culture = culture:irish
						capital_province = { geographical_region = custom_ireland }
					}
					add_achievement_global_variable = {
						VARIABLE = started_the_emerald_isle_achievement
						VALUE = yes
					}
				}
				if = {
					limit = {
						OR = {
							culture = culture:castilian
							culture = culture:basque
							culture = culture:portuguese
							culture = culture:catalan
							culture = culture:andalusian
							culture = culture:visigothic
							culture = culture:suebi
						}
						has_religion = religion:islam_religion
						capital_province = { geographical_region = world_europe_west_iberia }
					}
					add_achievement_global_variable = {
						VARIABLE = started_al_andalus_achievement
						VALUE = yes
					}
				}
				if = {
					limit = { 
						exists = character:159137
						this = character:159137
					}
					add_achievement_global_variable = {
						VARIABLE = started_almost_there_achievement
						VALUE = yes
					}
				}
				if = {
					limit = { 
						exists = character:109607
						this = character:109607
					}
					add_achievement_global_variable = {
						VARIABLE = started_last_count_first_king_achievement
						VALUE = yes
					}
				}
				if = {
					limit = { 
						exists = character:6878
						this = character:6878
					}
					add_achievement_global_variable = {
						VARIABLE = started_going_places_achievement
						VALUE = yes
					}
				}
			}
		}
	}

	events = {
		game_rule.1000	#Autopopulate families.
		game_rule.1011	#Hungarian Migration management.
	}
}
